using lwzlz.Application.System;
using lwzlz.Application.System.Dto;
using lwzlz.Web.Areas.SysAdmin.Models;
using lwzlz.Web.Filter;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [MenuFilter]
    public class CommodityController : AdminController
    {
        public ActionResult Index()
        {
            ViewBag.CategoryList = new CategoryService().GetList();
            return View();
        }

        /// <summary>
        /// 获取首页列表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [MenuRoute("Index")]
        public JsonResult GetListPage()
        {
            LayTableModel result = new LayTableModel();
            CommodityService service = new CommodityService();
            try
            {
                int page = int.Parse(Request.Params["page"]);//当前页码
                int limit = int.Parse(Request.Params["limit"]);//每页数量
                string field = Request.Params["field"];//排序字段名
                string order = Request.Params["order"];//排序方式：desc, asc, null(空对象，默认排序)
                //参数
                Dictionary<string, string> dic = new Dictionary<string, string>();
                string[] par = { "Name", "CommodityCode", "CategoryId", "Number", "Money" };

                foreach (string item in par)
                {
                    if (!string.IsNullOrWhiteSpace(Request.Params[item]))
                    {
                        dic.Add(item, Request.Params[item]);
                    }
                }
                int count = 0;

                var list = service.GetListPage(page, limit, dic, field, order, out count);

                result.data = list;
                result.count = count;

            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 添加修改视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult AddOrEdit(int Id = 0)
        {
            var model = new CommodityOutput();
            CommodityService service = new CommodityService();
            if (Id > 0)
            {
                model = service.Get<CommodityOutput>(Id);
            }
            ViewBag.CategoryList = new CategoryService().GetList();
            return View(model);
        }

        /// <summary>
        /// 查看视图
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [MenuRoute("Index")]
        public ActionResult Look(int Id = 0)
        {
            CommodityOutput model = null;
            CommodityService service = new CommodityService();
            if (Id > 0)
            {
                model = service.Get<CommodityOutput>(Id);
            }
            else
            {
                model = new CommodityOutput();
            }
            return View(model);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Add(CommodityInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                model.Code = "COM" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(0, 99999).ToString("00000");//COM2019090815162525548562
                int result = new CommodityService().Add(model);
                if (result > 0)
                {
                    response.Success = true;
                    response.Message = "添加成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "添加失败";
                }

            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult Edit(CommodityInput model)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                int id = new CommodityService().Edit(model);
                response.Success = true;
                response.Message = "修改成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="IdList"></param>
        /// <returns></returns>
        [MenuRoute("Delete")]
        public JsonResult DeleteList(string IdList)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                new CommodityService().DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }
    }
}
